﻿package com.flashwiitanks
{
	import flash.display.*;
	import flash.events.*;
	
	public class Bullet extends MovieClip
	{
		var parentTank:MovieClip;
		
		var main:MovieClip;
		
		var destroyParent:Boolean;
		
		var targetX:Number;
		var targetY:Number;
		
		var xMovement:Number;
		var yMovement:Number;
		
		var speed:Number = 10;
		
		var angle:Number;
		
		var collisions:Number = 1;
		
		public function Bullet(firingTank:MovieClip) {
			//get the parentTank
			parentTank = firingTank;
			
			destroyParent = false;
			
			//get the main stage
			main = MovieClip(parentTank.parent);
			
			this.rotation = parentTank.gun.rotation + parentTank.rotation;
			
			//position the bullet near the tank			
			if(this.rotation > 90) {
				this.x = -Math.cos(((180 - this.rotation) * Math.PI) / 180) * 30;
				this.y = Math.sin(((180 - this.rotation) * Math.PI) / 180) * 30;
			} else if (this.rotation < -90) {
				this.x = -Math.cos(((-180 + this.rotation) * Math.PI) / 180) * 30;
				this.y = -Math.sin(((-180 + this.rotation) * Math.PI) / 180) * 30;
			} else {
				this.x = Math.cos((this.rotation * Math.PI) / 180) * 30;
				this.y = Math.sin((this.rotation * Math.PI) / 180) * 30;
			}
				
			this.x += parentTank.x;
			this.y += parentTank.y;
			
			targetX = parentTank.parent.mouseX;
			targetY = parentTank.parent.mouseY;
			
			
			//when bullet is added to the stage
			this.addEventListener(Event.ADDED, initialize)

			//add bullet to stage
			main.addFire(this);
		}
		
		function initialize(event:Event):void {
			//this.rotation returns radians
			angle = this.rotation / 180 * Math.PI;
			xMovement = Math.cos(angle) * speed;
			yMovement = Math.sin(angle) * speed;
			
			//when the bullets enters a frame, it should move
			this.addEventListener(Event.ENTER_FRAME, moveBullet);
		}
		
		//on each from, move the bullet
		function moveBullet(event:Event):void {
			
			//check if hitting another bullet/missle
			//loop through all the bullets and missles
			for(var i:int = 0; i < main.fire.numChildren; i++) {
				//if colliding with another bullet/missle
				if(this.hitTestObject(main.fire.getChildAt(i))) {
					//both bullets will detect the collision
					//so only one should remove both objects
					//if this has a lower index, then remove the two colliding bullets/missles
					if(main.fire.getChildIndex(this) < main.fire.getChildIndex(main.fire.getChildAt(i))) {
						main.fire.getChildAt(i).destroy();
						this.destroy();
						return;
					}
				}
			}
			
			for(i = 0; i < main.walls.numChildren; i++) {
				var wall:MovieClip = main.walls.getChildAt(i);
				
				//if hitting a wall
				if(this.hitTestObject(wall)) {
					var left:Number = wall.x - (wall.width / 2);
					var right:Number = wall.x + (wall.width / 2);
					var top:Number = wall.y - (wall.height / 2);
					var bottom:Number = wall.y + (wall.height / 2);
					
					if(this.x > left && this.x < right) {
						if(collisions > 0) {
							yMovement *= -1;
							this.rotation *= -1;
							collisions--;
						} else {
							this.destroy();
							return;
						}
					}
					
					if(this.y > top && this.y < bottom) {
						if(collisions > 0) {
							xMovement *= -1;
							this.rotation *= -1;
							this.rotation += 180;
							collisions--;
						} else {
							this.destroy();
							return;
						}
					}
				}
			}
			
			for(i = 0; i < main.enemyTanks.numChildren; i++) {
				if(this.hitTestObject(main.enemyTanks.getChildAt(i))) {
					this.destroy();
					
					//kill the tank
					main.enemyTanks.getChildAt(i).kill();
					
					return;
				}
			}
			
			//if hitting player tank
			if(this.hitTestObject(parentTank) && destroyParent) {
				this.destroy();
				parentTank.kill();
				return;
			} else if (!this.hitTestObject(parentTank) && !destroyParent) {
				destroyParent = true;
			}
			
			/*
			trace('x = ' + this.x);
			trace('frontX = ' + this.frontX());
			trace('y = ' + this.y);
			trace('frontY = ' + this.frontY());
			*/
			
			//if hitting left or right wall
			if(this.frontX() <= 0 || this.frontX() >= stage.stageWidth) {
				if(collisions > 0) {
					/*
					trace('---- bounce ----');
					trace('x = '+ this.x);
					trace('y = ' + this.y);
					trace('rotation = ' + this.rotation);
					trace('width = ' + this.width);
					trace('height = ' + this.height);
					this.suspend();
					*/
					
					xMovement *= -1;
					this.rotation *= -1;
					this.rotation += 180;
					collisions--;
				} else {
					this.destroy();
					return;
				}
			}
			
			//if hitting top or bottom
			if(this.frontY() <= 0 || this.frontY() >= stage.stageHeight) {
				if(collisions > 0) {
					/*
					trace('---- bounce ----');
					trace('x = '+ this.x);
					trace('y = ' + this.y);
					trace('rotation = ' + this.rotation);
					trace('width = ' + this.width);
					trace('height = ' + this.height);
					this.suspend();
					*/
					
					yMovement *= -1;
					this.rotation *= -1;
					collisions--;
				} else {
					this.destroy();
					return;
				}
			}
			
			//move bullet
			this.x += xMovement;
			this.y += yMovement;
		}
		
		//when bullet is destroyed
		function destroy():void {
			this.removeEventListener(Event.ENTER_FRAME, moveBullet);
			parentTank.increaseClip();
			main.removeFire(this);
		}
		
		public function suspend():void {
			this.removeEventListener(Event.ENTER_FRAME, moveBullet);
		}
		
		function resume():void {
			this.addEventListener(Event.ENTER_FRAME, moveBullet);
		}
		
		function frontX():Number {
			//position the bullet near the tank			
			if(this.rotation > 90) {
				return -Math.cos(((180 - this.rotation) * Math.PI) / 180) * (this.width / 2) + this.x;
			} else if (this.rotation < -90) {
				return -Math.cos(((-180 + this.rotation) * Math.PI) / 180) * (this.width / 2) + this.x;
			} else {
				return Math.cos((this.rotation * Math.PI) / 180) * (this.width / 2)+ this.x;
			}
		}
		
		function frontY():Number {
			//position the bullet near the tank			
			if(this.rotation > 90) {
				return Math.sin(((180 - this.rotation) * Math.PI) / 180) * (this.width / 2) + this.y;
			} else if (this.rotation < -90) {
				return -Math.sin(((-180 + this.rotation) * Math.PI) / 180) * (this.width / 2) + this.y;
			} else {
				return Math.sin((this.rotation * Math.PI) / 180) * (this.width / 2) + this.y;
			}
		}
	}
}